package com.niiwoo.civet.user.service.local.sms.impl;

import com.niiwoo.civet.user.constants.LoginAccountErrorCode;
import com.niiwoo.civet.user.dao.entity.LoginAccountMapping;
import com.niiwoo.civet.user.dto.request.RefreshSMSCodeRequestDTO;
import com.niiwoo.civet.user.dto.response.UserBasicInfoResponseDTO;
import com.niiwoo.civet.user.enums.UserEnum;
import com.niiwoo.civet.user.enums.UserSMSTypeEnum;
import com.niiwoo.civet.user.service.local.UserBasicInfoService;
import com.niiwoo.civet.user.service.local.UserLoginAccountService;
import com.niiwoo.civet.user.service.local.sms.LimitedSMSSender;
import com.niiwoo.tripod.provider.exception.BizException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class RegisterSMSSender extends LimitedSMSSender {

    @Autowired
    UserLoginAccountService userLoginAccountService;

    @Autowired
    UserBasicInfoService userBasicInfoService;

    @Override
    protected void innerBeforeSend(RefreshSMSCodeRequestDTO requestDTO) {
        LoginAccountMapping mapping = userLoginAccountService.getAccountMapping(privacyMasks.encryptPrivacy(requestDTO.getLoginMobileNo()));
        if (mapping != null) {
            UserBasicInfoResponseDTO responseDTO = userBasicInfoService.getUserBaseInfo(mapping.getUserId());
            if (responseDTO == null || UserEnum.RegisterChannel.isPlatformChannel(responseDTO.getRegisterChannel())) {
                throw new BizException(LoginAccountErrorCode.USERS_ALREADY_EXIST);
            } else {
                throw new BizException(LoginAccountErrorCode.THIRD_PART_USERS_ALREADY_EXIST);
            }
        }
    }

    @Override
    protected String innerContentTemplate() {
        return REGISTER_TEMPLATE;
    }


    @Override
    protected UserSMSTypeEnum getType() {
        return UserSMSTypeEnum.REGISTER;
    }
}
